package com.tabnet.servlet.client;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tabnet.beans.commande.Commande;
import com.tabnet.beans.personnes.Client;
import com.tabnet.dao.DAOCommande;
import com.tabnet.dao.DAOException;
import com.tabnet.dao.DAOFactory;

public class CommandeTerminee extends HttpServlet{

		private static final long serialVersionUID = 1L;
	
	    public static final String SESSION_USER 		= "sessionClient";
	    public static final String SESSION_COMMANDES 	= "commandes";
	    public static final String VUE_SUCCES      		= "/adminClient/historique.jsp";
	    public static final String VUE        			= "/WEB-INF/connexionClient.jsp";
	    
	    private DAOCommande _CommandeDao;
	    
	    public void init() throws ServletException {
	        /* Récupération d'une instance de notre DAO Utilisateur */
	    	try {
				this._CommandeDao = DAOFactory.getInstance().getCommandeDAO();
			}
	    	catch (DAOException e) {
				e.printStackTrace();
			}
	    }
	    
	    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
	    	 /* Récupération du client dans la session */
	        HttpSession session = request.getSession();
	        
	    	Client client = (Client) session.getAttribute(SESSION_USER);
	    	
	    	if(client == null)
	            this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
	    	
	    	Map<String, Commande> commandes = null;
	    	
			try {
				commandes = this._CommandeDao.getCommandesTerminees(client.getId());
			}
			catch (DAOException e) {
				e.printStackTrace();
			}

			//Envoie des commandes dans la session
            session.setAttribute( SESSION_USER, client );
            session.setAttribute( SESSION_COMMANDES, commandes );

            this.getServletContext().getRequestDispatcher( VUE_SUCCES ).forward( request, response );
	    }

	    public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
	    	this.doGet(request, response);
	    }
}
